//
//  SPTHandler.h
//  NtoccDriverNew
//
//  Copyright © 2021 NYS. All rights reserved.
//  Created by niyongsheng.github.io on 2021/11/10.
//

#import <Foundation/Foundation.h>

#define SPT_Delay_Time      (30 * 1000)
#define SPT_Time_Interval   30.f
#define SPTTimeIntervalKey  @"SPTTimeIntervalKey"

#define KSPTHandler [SPTHandler sharedSPTHandler]
#define NRootViewController [UIApplication sharedApplication].delegate.window.rootViewController
#define NDLog(fmt, ...) { UIAlertController* alert = [UIAlertController alertControllerWithTitle:\
                            [NSString stringWithFormat:@"%s\n [Line %d] ", __PRETTY_FUNCTION__, __LINE__]\
                            message:[NSString stringWithFormat:fmt, ##__VA_ARGS__] preferredStyle:UIAlertControllerStyleAlert];\
                            [alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]];\
                            [[UIApplication sharedApplication].delegate.window.rootViewController presentViewController:alert animated:YES completion:nil];}

typedef NS_ENUM(NSUInteger, SPTStage) {
    SPTStageAuth,     // 初始化
    SPTStageStart,    // 开始
    SPTStageSend,     // 发送
    SPTStagePause,    // 暂停
    SPTStageRestart,  // 恢复
    SPTStageStop,     // 停止
    SPTStageOther     // 其他
};

@interface SPTHandler : NSObject

+ (instancetype)sharedSPTHandler;

/// 当前位置
@property (nonatomic, strong) CLLocation *location;
@property (nonatomic, strong) NSString *address;

/// 是否上报
@property (nonatomic, assign, readonly) BOOL isReport;
/// 上报信息(需自己维护)
@property (nonatomic, strong) NSMutableArray *shippingNoteInfos;

/// 刷新用户信息缓存
- (void)updateUserInfo;

/// 检查是否初始化SDK（后台配置）
- (void)configSPTHandlerWithBranchCode:(NSString*)branchCode;
- (void)configSPTHandlerWithBranchCode:(NSString*)branchCode completed:(void(^)(bool isSuccess, NSInteger code, NSString *msg))completed;

/// 检查后台持续定位权限
- (void)locationPermissionCheck:(void(^)(bool permit))completed;

/// 检查当前时间是否允许上报
- (void)checkInterval;

/// 查询当前订单
- (void)getCurrentOrder:(NSString *)remark isRecord:(BOOL)isRecord;
- (void)getCurrentOrder:(NSString *)remark isRecord:(BOOL)isRecord isRestart:(BOOL)isRestart completed:(void(^)(bool isSuccess, id model))completed;
/// 上报记录
- (void)orderReportRecord:(id)model stage:(SPTStage)stage stageStr:(NSString *)stageStr dataArray:(NSArray *)dataArray completed:(void(^)(bool isSuccess))completed;

/// 查询订单详情
/// @param codes 上报单号（多个单号以,分割）
/// @param stage 阶段
/// @param remark 备注
- (void)getOrderDetailWithCode:(NSString *)codes stage:(SPTStage)stage remark:(NSString *)remark;

/// 停止自动上报
- (void)cancelAutoSendLocation:(BOOL)isClearCache;

/// 测试省平台环境
- (void)testSpt;


#pragma mark - 上报钩子
- (void)appWillLogin:(NSNotification *)notifi;
- (void)appWillLogout:(NSNotification *)notifi;
- (void)applicationWillTerminate;

#pragma mark - 上报服务
/// 初始化SDK
- (void)openServiceWithAppId:(NSString *)appId appSecurity:(NSString *)appSecurity enterpriseSenderCode:(NSString *)enterpriseSenderCode environment:(NSString *)environment isRecord:(BOOL)isRecord listener:(void(^)(id model))listener;
/// 开始
- (void)startLocationWithRemark:(NSString*)remark branchCode:(NSString*)branchCode isRecord:(BOOL)isRecord listener:(void(^)(id model))listener;
- (void)startLocationWithShippingNoteInfos:(NSArray *)shippingNoteInfos remark:(NSString*)remark branchCode:(NSString*)branchCode isRecord:(BOOL)isRecord listener:(void(^)(id model))listener;
/// 发送
- (void)sendLocationWithDataArray:(NSMutableArray *)dataArray remark:(NSString*)remark isRecord:(BOOL)isRecord listener:(void(^)(id model))listener;
/// 暂停
- (void)pauseLocationWithRemark:(NSString*)remark branchCode:(NSString*)branchCode isRecord:(BOOL)isRecord listener:(void(^)(id model))listener;
- (void)pauseLocationWithShippingNoteInfos:(NSArray *)shippingNoteInfos remark:(NSString*)remark branchCode:(NSString*)branchCode isRecord:(BOOL)isRecord listener:(void(^)(id model))listener;
/// 恢复
- (void)reStartLocationWithRemark:(NSString*)remark branchCode:(NSString*)branchCode isRecord:(BOOL)isRecord listener:(void(^)(id model))listener;
- (void)reStartLocationWithShippingNoteInfos:(NSArray *)shippingNoteInfos remark:(NSString*)remark branchCode:(NSString*)branchCode isRecord:(BOOL)isRecord listener:(void(^)(id model))listener;
/// 结束
- (void)stopLocationWithRemark:(NSString*)remark branchCode:(NSString*)branchCode isRecord:(BOOL)isRecord listener:(void(^)(id model))listener;
- (void)stopLocationWithShippingNoteInfos:(NSArray *)shippingNoteInfos remark:(NSString*)remark branchCode:(NSString*)branchCode isRecord:(BOOL)isRecord listener:(void(^)(id model))listener;
/// 清空缓存
- (void)mapClear;
@end

